using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace BTV.Forms.Components
{
    public class RssParser
    {
        private RssObject _items;

        public RssObject Items
        {
            get { return _items; }
            set { _items = value; }
        }

        private string _url;

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings")]
        public string Url
        {
            get { return _url; }
            set { _url = value; }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "fnfe"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1807:AvoidUnnecessaryStringCreation", MessageId = "stack0")]
        public RssObjectCollection ParseFeed()
        {
            try
            {
                RssObjectCollection ret = new RssObjectCollection();
                XmlTextReader r = new XmlTextReader(Url);
                String currentTag = "";
                RssObject obj = new RssObject();
                while (r.Read())
                {
                    r.MoveToContent();
                    switch (r.NodeType)
                    {
                        case XmlNodeType.Element:
                            currentTag = r.Name.ToLower();
                            switch (currentTag)
                            {
                                case "item":
                                    obj = new RssObject();
                                    break;
                            }
                            break;
                        case XmlNodeType.Text:
                        case XmlNodeType.CDATA:
                            switch (currentTag)
                            {
                                case "title":
                                    obj.Title = r.Value;
                                    break;
                                case "link":
                                    obj.Link = r.Value;
                                    break;
                                case "description":
                                    obj.Description = r.Value;
                                    break;
                            }
                            break;
                        case XmlNodeType.EndElement:
                            switch (r.Name.ToLower())
                            {
                                case "item":
                                    ret.Add(obj);
                                    break;
                            }
                            break;
                    }
                }
                return ret;
            }
            catch
            {
                RssObjectCollection ret = new RssObjectCollection();
                RssObject obj = new RssObject();
                obj.Title = "Error Getting RSS Feed";
                obj.Description = "";
                obj.Link = Url;
                ret.Add(obj);
                return ret;
            }
        }
    }
}
